package com.bf.electroplating.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

/**
 * HTTP工具类
 * 
 * @author gzd
 * @since 2025-09-22
 */
@Slf4j
public class HttpUtil {

    private static final RestTemplate restTemplate = new RestTemplate();

    /**
     * 发送POST请求
     * 
     * @param url 请求URL
     * @param requestBody 请求体
     * @return 响应结果
     */
    public static String post(String url, String requestBody) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            
            HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
            
            log.info("HTTP请求成功，URL：{}，响应状态：{}", url, response.getStatusCode());
            return response.getBody();
        } catch (Exception e) {
            log.error("HTTP请求异常，URL：{}", url, e);
            throw new RuntimeException("HTTP请求失败", e);
        }
    }
}
